package cron

import (
	"bigdevops/src/models"
	"context"
	"fmt"
	"go.uber.org/zap"
)

// renew map方式

func (ac *AlertCache) RenewMapSendGroup(ctx context.Context) {

	//ac.Sc.Logger.Info("RenewMapSendGroup 启动....")

	// 先从数据库中查询出所有
	sendGroups, err := models.GetMonitorSendGroupAll()
	if err != nil {
		ac.Sc.Logger.Error("[缓存刷新模块]扫描数据库中的sendGroups失败",
			zap.Error(err),
		)
		return
	}
	if len(sendGroups) == 0 {
		return
	}

	tmpM := map[string]*models.MonitorSendGroup{}
	for _, sendGroup := range sendGroups {
		sendGroup := sendGroup
		sendGroup.FillFrontAllData()
		tmpM[fmt.Sprintf("%d", sendGroup.ID)] = sendGroup
	}

	// 模拟 数据量很大，刷新缓存很慢
	//time.Sleep(30 * time.Second)

	ac.SendGroupLock.Lock()
	lastNum := len(ac.SendGroupMap)
	ac.SendGroupMap = tmpM
	thiNum := len(ac.SendGroupMap)

	if !ac.StartFinishRenew {
		close(ac.cacheHasSynced)
		ac.StartFinishRenew = true
	}

	ac.SendGroupLock.Unlock()
	ac.Sc.Logger.Info("RenewMapSendGroup 刷新缓存结果....",
		zap.Any("上一次数量", lastNum),
		zap.Any("这一次数量", thiNum),
	)

}

func (ac *AlertCache) GetSendGroupById(id string) *models.MonitorSendGroup {

	ac.SendGroupLock.RLock()
	defer ac.SendGroupLock.RUnlock()
	return ac.SendGroupMap[id]
}
